﻿using System;
using System.Collections.Specialized;
using System.Configuration;

namespace TSites.Utility
{
	public class AppConfigReader : IConfigReader
	{
		private NameValueCollection _config = ConfigurationManager.AppSettings;

		#region IConfigReader Members

		public bool GetBoolValue(string key)
		{
			string strValue = GetStringValue(key, "0");
			return strValue == "1" || string.Equals(strValue, "true", StringComparison.OrdinalIgnoreCase);
		}

		public string GetStringValue(string key)
		{
			string retValue = _config[key];
			if (retValue != null)
			{
				retValue = retValue.Trim();
			}
			return retValue;
		}

		public string GetStringValue(string key, string defaultValue)
		{
			string retValue = GetStringValue(key);
			if (retValue == null)
			{
				retValue = defaultValue;
			}
			return retValue;
		}

		public int GetIntValue(string key)
		{
			int retValue;
			string strValue = GetStringValue(key, "0");
			if (!int.TryParse(strValue, out retValue))
			{
				retValue = 0;
			}
			return retValue;
		}

		#endregion
	}
}
